package com.qf.shopcar.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.qf.common.aop.annotation.LoginUser;
import com.qf.common.result.ResultEntity;
import com.qf.common.constant.RabbitMqConstants;
import com.qf.common.utils.JwtUtils;
import com.qf.entity.Car;
import com.qf.entity.User;
import com.qf.shopcar.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.naming.Name;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/carController")
public class CarController {
    @Autowired
    private CarService carService;
    @Autowired
    private RedisTemplate redisTemplate;

    // 把rediskey的序列化方式市值为字符串
    @PostConstruct
    public void init() {
        redisTemplate.setKeySerializer(RedisSerializer.string());
    }

    //购物车的增上改查要判断是否登录，有没有token
    @RequestMapping("/addCar")
    //自定义注解，表示这个方法需要验证当前用户是否登录，如果登录就注入当前用户，没有就注入null
    @LoginUser
    public ResultEntity addCar(Car car, User user, HttpServletResponse response,
                               @RequestHeader(name = RabbitMqConstants.LOGIN_TOKEN, required = false) String token
                               ) {
        System.out.println("CarController.addCar");
//        Boolean result = false;
//        @CookieValue(name = RabbitMqConstants.CAR_ANNO_NAME, required = false) String annoId
//        if (user != null) {
//            car.setUid(user.getId());
//            car.setCreateTime(new Date());
//            result = carService.insert(car);
//        } else {
//            // hset feidl(匿名用户ID) key(购买商品id) value(商品的数量)
//            // hset  00000001 5 1
//            //未登录的用户，把购物车数据存到redis里面
//            //annoid匿名用户id
//            if (StringUtils.isEmpty(annoId)) {
//                annoId = UUID.randomUUID().toString();
//                //创建cookies
//                Cookie cookie = new Cookie(RabbitMqConstants.CAR_ANNO_NAME, annoId);
//                //设置最大生存时间
//                cookie.setMaxAge(60 * 60 * 24 * 7);
//                //解决跨域问题
//                cookie.setPath("/");
//                //把cookie响应出去
//                response.addCookie(cookie);
//            }
//            //存到redis,
//            redisTemplate.opsForHash().put(annoId, car.getGid(), car.getCount());
//            //没有返回值
//            result = redisTemplate.opsForHash().hasKey(annoId, car.getGid());
//        }
//        return ResultEntity.success(result);
        return ResultEntity.success();
    }

    @RequestMapping("/delCar")
    public ResultEntity delCar(Car car,User user,@RequestHeader(name = RabbitMqConstants.LOGIN_TOKEN, required = false) String token
                               ) {
        System.out.println("CarController.delCar");
        return ResultEntity.success();
    }
}
